package com.zt.rongda.group.controller;

import com.zt.servlet.*;
import com.zt.rongda.group.model.EmployeeGroup;
import com.zt.rongda.group.service.EmployeeGroupService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/employeeGroup")
public class EmployeeGroupController {

    @Autowired
    private EmployeeGroupService employeeGroupService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(employeeGroupService.getTable(reqBean));
    }

    @RequestMapping(value = "/groupList")
    @ResponseBody
    public JSONObject groupList(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(employeeGroupService.groupList(reqBean));
    }

    @RequestMapping(value = "/empTree")
    @ResponseBody
    public JSONObject empTree(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(employeeGroupService.empTree(reqBean));
    }

    @RequestMapping(value = "/getEmpListByGroupId")
    @ResponseBody
    public JSONObject getEmpListByGroupId(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(employeeGroupService.getEmpListByGroupId(reqBean.getInfo("groupId")));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeGroupService.add(getEmployeeGroup(reqBean));
        return Response.responseSuccess();
    }


    @RequestMapping(value = "/saveMember")
    @ResponseBody
    public JSONObject saveMember(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeGroupService.saveMember(reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/updateMember")
    @ResponseBody
    public JSONObject updateMember(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeGroupService.updateMember(reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeGroupService.update(getEmployeeGroup(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeGroupService.delete(reqBean.getInfo("groupId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/deleteMember")
    @ResponseBody
    public JSONObject deleteMember(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        employeeGroupService.deleteMember(reqBean);
        return Response.responseSuccess();
    }



    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private EmployeeGroup getEmployeeGroup(ReqBean reqBean) {
        EmployeeGroup employeeGroup = new EmployeeGroup();
        employeeGroup.setGroupId(reqBean.getInfo("groupId"));
        employeeGroup.setGroupName(reqBean.getInfo("groupName"));
        employeeGroup.setStatus(reqBean.getInfo("status"));
        employeeGroup.setOperator(reqBean.getUserId());
        employeeGroup.setRemark(reqBean.getInfo("remark"));
        return employeeGroup;
    }

}